﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using ERP_RORZE_SYSTEM;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.Assembly
{
    public partial class frm_CheckFinished_F5Plan : Form
    {
        private INISettings mySetting;
        private SqlConnection myConnection;
        private SqlConnection masterConnection;

        private DataSet ds;
        private DataViewManager dsView;

        private DataTable resultTable;
        
        public frm_CheckFinished_F5Plan()
        {
            InitializeComponent();
            this.mySetting = new INISettings();
            this.myConnection = new SqlConnection(mySetting.SQLConnectionString);
            this.masterConnection = new SqlConnection(mySetting.DB2ConnectionString);
            this.ds = new DataSet("F5Plan");

            this.resultTable = new DataTable();
            this.resultTable.Columns.Add("OrderNo", typeof(string));
            this.resultTable.Columns.Add("PartNo", typeof(string));
            this.resultTable.Columns.Add("CommandQuantity", typeof(int));
            this.resultTable.Columns.Add("FinishedDate", typeof(DateTime));
            this.resultTable.Columns.Add("FinishedQuantity", typeof(int));
            this.resultTable.Columns.Add("FromSerialNo", typeof(string));
            this.resultTable.Columns.Add("ToSerialNo", typeof(string));
            this.resultTable.Columns.Add("FromMAC", typeof(string));
            this.resultTable.Columns.Add("ToMAC", typeof(string));
            this.resultTable.Columns.Add("CheckPerson", typeof(string));
            this.resultTable.Columns.Add("ExportDate", typeof(DateTime));
            this.resultTable.Columns.Add("RoHS", typeof(string));

            this.gridFinishedReport.DataSource = this.resultTable;
            this.FinishedReport.BestFitColumns();

            LoadData();

            this.quantityValue.TextChanged += quantityValue_TextChanged;            
            this.fromSerial.LostFocus += fromSerial_LostFocus;            
            this.finishedQuantity.LostFocus += finishedQuantity_LostFocus;
            this.MACSerialFrom.LostFocus += MACSerialFrom_LostFocus;
            this.remarkValue.KeyPress += remarkValue_KeyPress;

            this.finishedDate.EditValue = DateTime.Now.Date;

            this.orderNo.SelectedIndexChanged += orderNo_SelectedIndexChanged;
            fromSerial.Text.ToUpper();
            MACSerialFrom.Text.ToUpper();
            toSerial.Text.ToUpper();
            MACSerialTo.Text.ToUpper();
        }

        void remarkValue_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.KeyChar = Char.ToUpper(e.KeyChar);
            if (e.KeyChar == (char)Keys.Enter)
            {                
                buttonAdd_Click(sender, e);
            }
        }

        void orderNo_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.rohs.Checked = true;
            this.fromSerial.Text = "";
            this.MACSerialFrom.Text = "";
            this.toSerial.Text = "";
            this.MACSerialTo.Text = "";
        }

        void MACSerialFrom_LostFocus(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(this.MACSerialFrom.Text.Trim()))
                {
                    MACSerialTo.Text = "";
                }
                if (!string.IsNullOrEmpty(this.finishedQuantity.Text.Trim()) && (!string.IsNullOrEmpty(this.MACSerialFrom.Text.Trim())))
                {
                    int finish_qty = 0;
                    if (Int32.TryParse(this.finishedQuantity.Text.Trim(), out finish_qty))
                    {
                        if (finish_qty > 0)
                        {
                            int start_number = Int32.Parse(this.MACSerialFrom.Text.Trim(), System.Globalization.NumberStyles.HexNumber);
                            int end_number = start_number + finish_qty - 1;
                            this.MACSerialTo.Text = end_number.ToString("X" + this.MACSerialFrom.Text.Trim().Length.ToString());
                        }
                    }
                    else
                    {
                        MessageBox.Show("Invalid finished quantity");
                    }
                }
                this.MACSerialTo.Text = this.MACSerialTo.Text.ToUpper();
            }
            catch
            {
                MessageBox.Show("Invalid From MAC Serial No");
            }
        }

        void finishedQuantity_LostFocus(object sender, EventArgs e)
        {
            try
            {
                if (haveSerial(this.robotNo.Text))
                {
                    if (this.fromSerial.Text.Length > 4)
                    {
                        if (!string.IsNullOrEmpty(this.finishedQuantity.Text))
                        {
                            int finish_qty = 0;
                            if (Int32.TryParse(this.finishedQuantity.Text, out finish_qty))
                            {
                                if (finish_qty > 0)
                                {
                                    string prefix = this.fromSerial.Text.Substring(0, this.fromSerial.Text.Length - 4);
                                    string first_number = this.fromSerial.Text.Substring(this.fromSerial.Text.Length - 4, 4);
                                    int start_number = 0;
                                    Int32.TryParse(first_number, out start_number);
                                    int end_number = start_number + finish_qty - 1;
                                    this.toSerial.Text = prefix + end_number.ToString("D4");
                                }
                            }
                            else
                            {
                                MessageBox.Show("Invalid Finished Quantity");
                            }
                        }
                        else
                        {
                            MessageBox.Show("Invalid Finished Quantity");
                        }
                    }
                    //else
                    //{
                    //    MessageBox.Show("Invalid Serial No");
                    //}
                }

                if (haveMACSerial(this.robotNo.Text))
                {
                    if (!string.IsNullOrEmpty(this.finishedQuantity.Text) && (!string.IsNullOrEmpty(this.MACSerialFrom.Text)))
                    {
                        int finish_qty = 0;
                        if (Int32.TryParse(this.finishedQuantity.Text, out finish_qty))
                        {
                            if (finish_qty > 0)
                            {
                                int start_number = Int32.Parse(this.MACSerialFrom.Text, System.Globalization.NumberStyles.HexNumber);
                                int end_number = start_number + finish_qty - 1;
                                this.MACSerialTo.Text = end_number.ToString("X");
                            }
                        }
                        else
                        {
                            MessageBox.Show("Invalid finished quantity");
                        }
                    }
                }
            }
            catch
            {
                MessageBox.Show("Invalid Finished Quantity");
            }
        }

        void fromSerial_LostFocus(object sender, EventArgs e)
        {
            try
            {
                if (haveSerial(this.robotNo.Text))
                {
                    if (this.fromSerial.Text.Length > 4)
                    {
                        if (!string.IsNullOrEmpty(this.finishedQuantity.Text))
                        {
                            int finish_qty = 0;
                            if (Int32.TryParse(this.finishedQuantity.Text, out finish_qty))
                            {
                                if (finish_qty > 0)
                                {
                                    string prefix = this.fromSerial.Text.Substring(0, this.fromSerial.Text.Length - 4);
                                    string first_number = this.fromSerial.Text.Substring(this.fromSerial.Text.Length - 4, 4);
                                    int start_number = 0;
                                    Int32.TryParse(first_number, out start_number);
                                    int end_number = start_number + finish_qty - 1;
                                    this.toSerial.Text = prefix + end_number.ToString("D4");
                                }
                            }
                            else
                            {
                                MessageBox.Show("Invalid Finished Quantity");
                            }
                        }
                        else
                        {
                            MessageBox.Show("Invalid Finished Quantity");
                        }
                    }
                    //else
                    //{
                    //    MessageBox.Show("Invalid Serial No");
                    //}
                }
                this.toSerial.Text = this.toSerial.Text.ToUpper();
            }
            catch
            {
                MessageBox.Show("Invalid From Serial No");
            }
        }

        
        void quantityValue_TextChanged(object sender, EventArgs e)
        {
            this.finishedQuantity.Text = this.quantityValue.Text;
        }

        ///////////////////////////////////////
        /// Load Database to Binding
        ///////////////////////////////////////
        private void LoadData()
        {
            try
            {
                myConnection.Open();
                SqlCommand cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT t1.OrderNo, t1.PartNo, t1.CommandQuantity, t1.DeadLine, t1.FinishedDate,"
                    + " t1.FinishedQuantity, t1.Remark, t1.FromSerialNo, t1.ToSerialNo, t2.PersonCheck"
                    + " FROM [231_F5Plan] AS t1 LEFT JOIN [231_Comp] AS t2 ON t1.PartNo=t2.CompID"
                    + " WHERE (t1.FinishedDate IS NULL)";                
                SqlDataAdapter da1 = new SqlDataAdapter();
                da1.SelectCommand = cmd_sql;
                da1.TableMappings.Add("Table", "F5PlanTable");
                da1.Fill(ds);
                dsView = ds.DefaultViewManager;
                cmd_sql.Dispose();
                myConnection.Close();

                this.orderNo.DataSource = dsView;
                this.orderNo.DisplayMember = "F5PlanTable.OrderNo";
                this.orderNo.ValueMember = "F5PlanTable.OrderNo";

                this.robotNo.DataBindings.Add("Text", dsView, "F5PlanTable.PartNo");
                this.quantityValue.DataBindings.Add("Text", dsView, "F5PlanTable.CommandQuantity");
                this.PersonCheck.DataBindings.Add("Text", dsView, "F5PlanTable.PersonCheck");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            string error = null;
            DateTime finished_date = new DateTime();
            string order = null;

            string from_serial = null;
            string to_serial = null;

            string from_MACSerial = null;
            string to_MACSerial = null;

            bool has_serial = haveSerial(this.robotNo.Text);
            bool has_MAC = haveMACSerial(this.robotNo.Text);

            if (this.orderNo.SelectedIndex < 0)
            {
                error = "Please select Order No";
            }
            else
            {
                order = this.orderNo.SelectedValue.ToString();
            }

                int checksubmit = 0;
                if (FinishedReport.RowCount > 0)
                {
                    for (int i = 0; i < FinishedReport.DataRowCount; i++)
                    {
                        if (FinishedReport.GetRowCellValue(i, "OrderNo").ToString() == orderNo.Text)
                        {
                            checksubmit = 1;
                        }
                    }
                }
                else
                {
                    checksubmit = 0;
                }
                if (checksubmit == 0)
                {
                    if (Int32.Parse(this.finishedQuantity.Text) < Int32.Parse(this.quantityValue.Text))
                    {
                        DialogResult dlgresult = MessageBox.Show("The finished quantity is less than command quantity. Do you want to submit?", "Waring!", MessageBoxButtons.YesNo);
                        if (dlgresult == System.Windows.Forms.DialogResult.No)
                        {
                            finishedQuantity.Text = quantityValue.Text;
                            return;
                        }
                    }

                    if (string.IsNullOrEmpty(error))
                    {
                        if (Int32.Parse(this.finishedQuantity.Text) > Int32.Parse(this.quantityValue.Text))
                        {
                            error = "The finished quantity cannot be greater than command quantity.";
                        }
                    }
                    if (string.IsNullOrEmpty(error))
                    {
                        if (string.IsNullOrEmpty(this.finishedDate.EditValue.ToString()))
                        {
                            error = "Please select Finished Date";
                        }
                        else
                        {
                            finished_date = this.finishedDate.DateTime;
                        }
                    }

                    if (string.IsNullOrEmpty(error))
                    {
                        try
                        {
                            if (has_serial)
                            {
                                if (string.IsNullOrEmpty(this.fromSerial.Text))
                                {
                                    error = "Please enter From Serial";
                                }
                                else
                                {
                                    if (this.fromSerial.Text.Length <= 4)
                                    {
                                        error = "Invalid From Serial.";
                                    }
                                    else
                                    {
                                        int i;
                                        if (!Int32.TryParse(this.fromSerial.Text.Substring(this.fromSerial.Text.Length - 4), out i))
                                        {
                                            error = "Invalid from serial";
                                        }
                                        else
                                        {
                                            if (checkSserialExist())
                                            {
                                                error = "This serial no has been existed. Please enter another serial no.";
                                            }
                                            else
                                            {
                                                from_serial = this.fromSerial.Text;
                                                to_serial = this.toSerial.Text;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        catch
                        {
                            error = "Invalid From Serial.";
                        }
                    }

                    if (string.IsNullOrEmpty(error))
                    {
                        try
                        {
                            if (has_MAC)
                            {
                                if (string.IsNullOrEmpty(this.MACSerialFrom.Text))
                                {
                                    error = "Please enter From MAC Serial";
                                }
                                else
                                {
                                    if (checkMACSserialExist())
                                    {
                                        error = "This MAC serial no has been existed. Please enter another MAC serial no.";
                                    }
                                    else
                                    {
                                        from_MACSerial = this.MACSerialFrom.Text;
                                        to_MACSerial = this.MACSerialTo.Text;
                                    }
                                }
                            }
                        }
                        catch
                        {
                            error = "Please enter From MAC Serial";
                        }
                    }

                    if (string.IsNullOrEmpty(error))
                    {
                        DataRow newrow = this.resultTable.NewRow();
                        newrow["OrderNo"] = order;
                        newrow["PartNo"] = this.robotNo.Text;
                        newrow["CommandQuantity"] = Int32.Parse(this.quantityValue.Text);
                        newrow["FinishedDate"] = finished_date.Date;
                        newrow["FinishedQuantity"] = Int32.Parse(this.finishedQuantity.Text);
                        newrow["FromSerialNo"] = this.fromSerial.Text;
                        newrow["ToSerialNo"] = this.toSerial.Text;
                        newrow["FromMAC"] = this.MACSerialFrom.Text;
                        newrow["ToMAC"] = this.MACSerialTo.Text;
                        /*
                            if (has_serial)
                        {
                            newrow["FromSerialNo"] = this.fromSerial.Text;
                            newrow["ToSerialNo"] = this.toSerial.Text;
                        }
                        else
                        {
                            if (has_MAC)
                            {
                                newrow["FromMAC"] = this.MACSerialFrom.Text;
                                newrow["ToMAC"] = this.MACSerialTo.Text;
                            }
                        }
                         */
                        newrow["CheckPerson"] = this.PersonCheck.Text;
                        newrow["ExportDate"] = finished_date.Date;
                        newrow["RoHS"] = this.rohs.Checked ? "OK" : "NG";
                        this.resultTable.Rows.Add(newrow);
                        orderNo.SelectedIndex = -1;
                        this.remarkValue.Text = "";
                        orderNo.Focus();
                    }
                    else
                    {
                        MessageBox.Show(error);
                    }
                }
                else { MessageBox.Show("You already submited this report! Please check again."); }
        }

        ////////////////////////////////////
        /// Check Have Serial
        ///////////////////////////////////
        private bool haveSerial(string compID)
        {
            bool ret = false;
            try
            {
                myConnection.Open();
                SqlCommand sql_cmd = myConnection.CreateCommand();
                sql_cmd.CommandText = "SELECT NormalSerial FROM [231_Comp] WHERE CompID='" + compID + "'";
                SqlDataReader dataReader = sql_cmd.ExecuteReader();
                if (dataReader.Read())
                {
                    if (dataReader["NormalSerial"] != DBNull.Value)
                    {
                        ret = (bool)dataReader["NormalSerial"];
                    }
                }
                dataReader.Close();
                myConnection.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                ret = true;
            }
            return ret;
        }

        ////////////////////////////////////
        /// Check Have MAC Serial
        ///////////////////////////////////
        private bool haveMACSerial(string compID)
        {
            bool ret = false;
            try
            {
                myConnection.Open();
                SqlCommand sql_cmd = myConnection.CreateCommand();
                sql_cmd.CommandText = "SELECT MACSerial FROM [231_Comp] WHERE CompID='" + compID + "'";
                SqlDataReader dataReader = sql_cmd.ExecuteReader();
                if (dataReader.Read())
                {
                    if (dataReader["MACSerial"] != DBNull.Value)
                    {
                        ret = (bool)dataReader["MACSerial"];
                    }
                }
                dataReader.Close();
                myConnection.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                ret = true;
            }
            return ret;
        }

        ////////////////////////////////////
        /// Check Exist Serial
        ///////////////////////////////////
        private bool checkSserialExist()
        {
            bool ret = false;
            try
            {
                myConnection.Open();
                SqlCommand sql_cmd = myConnection.CreateCommand();
                sql_cmd.CommandText = "SELECT COUNT(ID) FROM [231_F5Detail] WHERE (SerialNo='" + this.fromSerial.Text + "'"
                    + " OR SerialNo='" + this.toSerial.Text + "') AND CompID='" + this.robotNo.Text + "'";
                SqlDataReader dataReader = sql_cmd.ExecuteReader();
                if (dataReader.Read())
                {
                    if (dataReader[0] != DBNull.Value)
                    {
                        if (Int32.Parse(dataReader[0].ToString()) > 0)
                        {
                            ret = true;
                        }
                    }
                }
                dataReader.Close();
                myConnection.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                ret = true;
            }
            return ret;
        }

        ////////////////////////////////////
        /// Check Exist MAC Serial
        ///////////////////////////////////
        private bool checkMACSserialExist()
        {
            bool ret = false;
            try
            {
                myConnection.Open();
                SqlCommand sql_cmd = myConnection.CreateCommand();
                sql_cmd.CommandText = "SELECT COUNT(ID) FROM [231_F5Detail] WHERE SerialNo='" + this.MACSerialFrom.Text + "'"
                    + " OR SerialNo='" + this.MACSerialTo.Text + "'";
                SqlDataReader dataReader = sql_cmd.ExecuteReader();
                if (dataReader.Read())
                {
                    if (dataReader[0] != DBNull.Value)
                    {
                        if (Int32.Parse(dataReader[0].ToString()) > 0)
                        {
                            ret = true;
                        }
                    }
                }
                dataReader.Close();
                myConnection.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                ret = true;
            }
            return ret;
        }

        private void buttonSend_Click(object sender, EventArgs e)
        {
            string error = null;
            if (this.resultTable.Rows.Count <= 0)
            {
                error = "There is no record to send";
            }

            if (string.IsNullOrEmpty(error))
            {
                if (string.IsNullOrEmpty(this.reportNo.Text))
                {
                    error = "Plase enter Report No.";
                }
                else
                {
                    if (FinishedReport.RowCount != Int32.Parse(this.reportNo.Text))
                    {
                        error = "The report number is not equal command record.";
                    }
                }
            }

            if (string.IsNullOrEmpty(error))
            {
                string prefix = null;
                int start_number = 0;
                int end_number = 0;
                int start_mac = 0;
                int end_mac = 0;
                string serial = null;
                string mac = null;
                bool checkserial = false;
                bool checkMAC = false;

                foreach (DataRow addrow in this.resultTable.Rows)
                {
                    checkserial = haveSerial(addrow["PartNo"].ToString());
                    checkMAC = haveMACSerial(addrow["PartNo"].ToString());
                    myConnection.Open();
                    SqlTransaction tran = myConnection.BeginTransaction();
                    SqlCommand sql_cmd;
                    try
                    {
                        string _fromSerialNo = "";
                        string _toSerialNo = "";
                        if (checkserial)
                        {
                            _fromSerialNo = addrow["FromSerialNo"].ToString();
                            _toSerialNo = addrow["ToSerialNo"].ToString();
                        }
                        else
                        {
                            if (checkMAC)
                            {
                                _fromSerialNo = addrow["FromMAC"].ToString();
                                _toSerialNo = addrow["ToMAC"].ToString();
                            }
                        }
                        sql_cmd = myConnection.CreateCommand();
                        sql_cmd.CommandText = "UPDATE [231_F5Plan] SET FinishedDate=@finisheddate,"
                            + " FinishedQuantity=" + addrow["FinishedQuantity"].ToString() + ","
                            + " FromSerialNo=N'" + _fromSerialNo + "',"
                            + " ToSerialNo=N'" + _toSerialNo + "'"
                            + " WHERE OrderNo='" + addrow["OrderNo"].ToString() + "'";
                        sql_cmd.Parameters.Add("@finisheddate",
                            SqlDbType.DateTime).Value = DateTime.Parse(addrow["FinishedDate"].ToString()).Date;
                        sql_cmd.Transaction = tran;
                        sql_cmd.ExecuteNonQuery();
                        sql_cmd.Dispose();

                        if (checkserial)
                        {
                            prefix = addrow["FromSerialNo"].ToString().Substring(0, addrow["FromSerialNo"].ToString().Length - 4);
                            start_number = Int32.Parse(addrow["FromSerialNo"].ToString().Substring(addrow["FromSerialNo"].ToString().Length - 4));
                            end_number = Int32.Parse(addrow["ToSerialNo"].ToString().Substring(addrow["FromSerialNo"].ToString().Length - 4));
                            if (checkMAC)
                            {
                                start_mac = Int32.Parse(addrow["FromMAC"].ToString(), System.Globalization.NumberStyles.HexNumber);
                            }


                            for (int i = start_number; i <= end_number; i++)
                            {
                                serial = prefix + i.ToString("D4");
                                if (checkMAC)
                                {
                                    mac = start_mac.ToString("X7");
                                }
                                else
                                {
                                    mac = null;
                                }
                                start_mac++;
                                sql_cmd = myConnection.CreateCommand();
                                sql_cmd.CommandText = "INSERT INTO [231_F5Detail] (OrderNo, SerialNo, CompID, FinishDate,"
                                        + " CheckPerson, ExportDate, RoHS, CustomerID, MACAddress)"
                                        + " VALUES (N'" + addrow["OrderNo"].ToString() + "', N'" + serial + "',"
                                        + " N'" + addrow["PartNo"].ToString() + "', @finisheddate,"
                                        + " N'" + addrow["CheckPerson"].ToString() + "', @finisheddate,"
                                        + " '" + addrow["RoHS"].ToString() + "', 'RRC', N'" + mac + "')";
                                sql_cmd.Transaction = tran;
                                sql_cmd.Parameters.Add("@finisheddate",
                                    SqlDbType.DateTime).Value = DateTime.Parse(addrow["FinishedDate"].ToString()).Date;
                                sql_cmd.ExecuteNonQuery();
                                sql_cmd.Dispose();
                            }
                        }
                        else
                        {
                            if (checkMAC)
                            {
                                start_mac = Int32.Parse(addrow["FromMAC"].ToString(), System.Globalization.NumberStyles.HexNumber);
                                end_mac = Int32.Parse(addrow["ToMAC"].ToString(), System.Globalization.NumberStyles.HexNumber);
                                for (int i = start_mac; i <= end_mac; i++)
                                {
                                    mac = i.ToString("X7");
                                    sql_cmd = myConnection.CreateCommand();
                                    sql_cmd.CommandText = "INSERT INTO [231_F5Detail] (OrderNo, SerialNo, CompID, FinishDate,"
                                        + " CheckPerson, ExportDate, RoHS, CustomerID)"
                                        + " VALUES (N'" + addrow["OrderNo"].ToString() + "', N'" + mac + "',"
                                        + " N'" + addrow["PartNo"].ToString() + "', @finisheddate,"
                                        + " N'" + addrow["CheckPerson"].ToString() + "', @finisheddate,"
                                        + " '" + addrow["RoHS"].ToString() + "', 'RRC')";
                                    sql_cmd.Transaction = tran;
                                    sql_cmd.Parameters.Add("@finisheddate",
                                        SqlDbType.DateTime).Value = DateTime.Parse(addrow["FinishedDate"].ToString()).Date;
                                    sql_cmd.ExecuteNonQuery();
                                    sql_cmd.Dispose();
                                }
                            }
                        }
                        tran.Commit();
                        myConnection.Close();
                    }
                    catch (Exception ex)
                    {
                        tran.Rollback();
                        if (myConnection.State == ConnectionState.Open)
                        {
                            myConnection.Close();
                        }
                        MessageBox.Show(ex.Message);
                    }
                }

                MessageBox.Show("Report has been sent");
                this.resultTable.Rows.Clear();
            }
            else
            {
                MessageBox.Show(error);
            }
        }

        private void FinishedReport_RowCountChanged(object sender, EventArgs e)
        {
            if (FinishedReport.RowCount > 0)
            {
                buttonRemove.Enabled = true;
            }
            else
            {
                buttonRemove.Enabled = false;
            }
        }

        private void buttonRemove_Click(object sender, EventArgs e)
        {
            FinishedReport.DeleteSelectedRows();
        }

        private void fromSerial_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.KeyChar = Char.ToUpper(e.KeyChar);
            if (e.KeyChar == (char)Keys.Enter)
            {
                fromSerial_LostFocus(sender, e);
                buttonAdd_Click(sender, e);
            }
        }

        private void MACSerialFrom_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.KeyChar = Char.ToUpper(e.KeyChar);
            if (e.KeyChar == (char)Keys.Enter)
            {
                MACSerialFrom_LostFocus(sender, e);
                buttonAdd_Click(sender, e);
            }
        }

        private void clearForm()
        {
            foreach (Control c in this.Controls)
            {
                if (c.Name != "finishedDate")
                {
                   
                    if (c is System.Windows.Forms.ComboBox)
                    {
                        ((System.Windows.Forms.ComboBox)c).SelectedIndex = -1;
                    }
                   
                    if (c is System.Windows.Forms.TextBox)
                    {
                        ((System.Windows.Forms.TextBox)c).Text = null;
                    }

                    if (c is DevExpress.XtraEditors.TextEdit)
                    {
                        ((DevExpress.XtraEditors.TextEdit)c).Text = null;
                    }

                    if (c is DevExpress.XtraEditors.ComboBoxEdit)
                    {
                        ((DevExpress.XtraEditors.ComboBoxEdit)c).SelectedIndex = -1;
                    }

                    if (c is System.Windows.Forms.MaskedTextBox)
                    {
                        ((System.Windows.Forms.MaskedTextBox)c).Text = null;
                    }
                }
            }
        }

        private void finishedQuantity_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                buttonAdd_Click(sender, e);
            }
        }

    }
}
